package main

import (
	"fmt"
	"go_code/chatroom/server/message"
	"go_code/chatroom/server/process"
	"go_code/chatroom/server/utils"
	"io"
	"net"
)

type Processor struct {
	Conn net.Conn
}

func (p *Processor) handle() (err error) {
	t := &utils.Transport{
		Conn: p.Conn,
	}

	var msg *message.Message

	for {
		msg, err = t.ReadPkg()
		if err != nil {
			if err == io.EOF {
				fmt.Println("客户端退出...")
				return
			}
			return
		}

		p.handleMessage(msg)
	}
}

func (p *Processor) handleMessage(msg *message.Message) {
	switch msg.Type {
	case message.TypeRegisterReq:
		userProcess := &process.UserProcess{Conn: p.Conn}
		_ = userProcess.Register(msg)
	case message.TypeLoginReq:
		userProcess := &process.UserProcess{Conn: p.Conn}
		_ = userProcess.Login(msg)
	case message.TypeSmsReq:
		smsProcess := &process.SmsProcess{}
		smsProcess.SendGroupMessageToOnlineUsers(msg)
	default:
		fmt.Println("找不到的类型")
	}
}
